package com.sunmouse.toolkit.fragment.fashion;


import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.sunmouse.toollib.utils.LogUtils;
import com.sunmouse.toolkit.R;
import com.sunmouse.toolkit.adapter.ItemAdapter;
import com.sunmouse.toolkit.fragment.BaseFragment;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class TitleListFragment extends BaseFragment implements ItemAdapter.OnViewChangedListener {


    @BindView(R.id.m_list)
    ListView mList;
    @BindView(R.id.title_tv)
    TextView titleTv;
    Unbinder unbinder;

    private ItemAdapter mAdapter;
    private ArrayList<String> data;
    private int lastIndex;
    private ValueAnimator inAnim;
    private ValueAnimator outAnim;

    public TitleListFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_title_list, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void init() {
        super.init();

        data = new ArrayList<>();
        this.mAdapter = new ItemAdapter(data, getActivity());
        this.mAdapter.setOnViewChangedListener(this);

        this.mList.setAdapter(mAdapter);


        loadingData(80);

//        init anim
        inAnim = getAnim(0.2f, 1.0f);
        outAnim = getAnim(1.0f, 0.0f);
    }


    private ValueAnimator getAnim(float... values) {
        ValueAnimator inAnim = ValueAnimator.ofFloat(values);
        inAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float alpha = (float) animation.getAnimatedValue();
                ViewCompat.setAlpha(titleTv, alpha);
            }
        });
        inAnim.setDuration(2 * 1000);

        return inAnim;
    }

    public void loadingData(int pageSize) {
        for (int i = 0; i < pageSize; i++) {
            if (i % 20 == 0) {
                data.add("title-" + (i / 20 + 1));
            }
            data.add("describe。。。。。。。。。。。。。。。。。");
        }

        mAdapter.notifyDataSetChanged();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onViewChanged(int position) {
        LogUtils.v("----p=" + position);
        int titleIndex = position / 20;
//        if (position % 20 == 0) {
//            this.titleTv.setVisibility(View.VISIBLE);
//            this.titleTv.setText(mData.get(position));
//        }

        if (position - lastIndex > 0) {
            showTitle(true);
        } else {
            showTitle(false);
        }
        if (data.get(position).startsWith("title-")) {
//            this.titleTv.setVisibility(View.VISIBLE);
            this.titleTv.setText(data.get(position));
        }

        lastIndex = position;
    }


    /**
     * show title
     */
    private void showTitle(boolean showStatus) {
        this.titleTv.setVisibility(showStatus ? View.VISIBLE : View.GONE);
        if (showStatus) {
            inAnim.start();
            return;
        }
        outAnim.start();
    }

}
